package com.zhitian.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.csvreader.CsvWriter;

public class Meiya {
	
	/**获取html文件
	 * @param path   Report文件夹路径
	 * @return
	 */
	public static List<String> getHtml(String path) {
		List<String> list = new ArrayList<>();
		boolean isMatch;
		String regex = "Contents.*.html";
		File input = new File(path);
		for(String string : input.list()) {
			isMatch = Pattern.matches(regex, string);
			if(isMatch) {
				list.add(string);
			}
		}
		
		return list;
	}
	
	/**
	 * 创建CsvWriter
	 * @param path  csv文件输出路径
	 * @param header  csv文件的header
	 * @return CsvWriter实例
	 * @throws IOException
	 */
	public static CsvWriter createCsvWriter(String path,String[] header) throws IOException {
		
		FileOutputStream fos = new FileOutputStream(path);
		//在文件中增加BOM，该处的byte[] 可以针对不同编码进行修改
		fos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
		CsvWriter writer = new CsvWriter(fos,',',Charset.forName("UTF-8"));
		//String[] header = {"序号","时间","动作","对方","内容"};
		writer.writeRecord(header);
		
		return writer;
	}
	/**
	 * 判断字符串是否全为数字
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str) {  
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
    }

	/**
	 * 关闭CsvWriter
	 * @param list  CsvWriter对象列表
	 */
	public static void  closeCsvWriter(List<CsvWriter> list) {
		for(CsvWriter csvWriter : list) {
			csvWriter.flush();
			csvWriter.close();
		}
		
	}
	//删除文件
	public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
	/**
	 * 爬取数据并导出csv
	 * @param path  处理的文件夹路径
	 * @param csvOutPath  csv导出目录
	 * @throws IOException 
	 */
	public static void outCsv(String path, String csvOutPath,boolean get_applist,boolean get_contacts,
			boolean get_callsRecord,boolean get_message,boolean get_wechat,boolean get_QQ,
			boolean get_alipay,boolean get_systemInfo) throws IOException {
		
		List<String> htmls = getHtml(path);
		
		//准备存储应用列表的csv
		String applist = csvOutPath+"应用列表.csv";
		CsvWriter applist_writer = createCsvWriter(applist, 
				new String[] {"序号","应用"});
		
		//通讯录csv
		String contacts = csvOutPath+"通讯录.csv";
		CsvWriter contacts_writer = createCsvWriter(contacts,
				new String[] {"序号","姓名","电话","群组","其他信息","删除？"});
		
		//通话记录
		String calls = csvOutPath+"通话记录.csv";
		CsvWriter calls_writer = createCsvWriter(calls, 
				new String[] {"序号","姓名","电话号码","通话时间","持续时间","是否删除","MD5"});
		
		//短信
		String message = csvOutPath+"短信.csv";
		CsvWriter message_writer = createCsvWriter(message, 
				new String[] {"发送者","内容","时间"});
		
		//微信账号信息
		String wx_account_info = csvOutPath+"微信账号信息.csv";
		CsvWriter wx_account_info_writer = createCsvWriter(wx_account_info, 
				new String[] {"序号","微信号","昵称","微信ID","QQ号码"
						,"密钥","性别","个性签名","备注","省","市","头像","邮箱地址","登录时间","是否删除","国家","手机号","MD5"});
		
		//微信好友统计
		String wx_friends = csvOutPath+"微信好友统计.csv";
		CsvWriter wx_friends_writer = createCsvWriter(wx_friends, 
				new String[] {"序号","微信ID","账号","昵称","性别","个性签名","备注","所在地","头像","QQ账号","QQ昵称","邮箱地址",
						"移动电话","是否删除"
						,"描述","标签","共同群聊","MD5"});
		//微信群组统计
		String wx_groups = csvOutPath+"微信群组统计.csv";
		CsvWriter wx_groups_writer = createCsvWriter(wx_groups, 
				new String[] {"序号","群账号","群名称","群主号码","群主昵称","成员个数","我在本群昵称","是否删除","MD5"});
		
		//微信群联系人
		String wx_group_contacts = csvOutPath+"微信群联系人.csv";
		CsvWriter wx_group_contacts_writer = createCsvWriter(wx_group_contacts, 
				new String[] {"序号","群账号","群名称","成员昵称","成员账号","是否删除","在本群的昵称","个性签名","所在地","头像","入群方式","MD5"});
		
		//无线连接账号信息
		String wifi_account_info = csvOutPath+"无限连接账号信息.csv";
		CsvWriter wifi_account_info_writer = createCsvWriter(wifi_account_info, 
				new String[] {"序号","MAC","名称","最近加入时间","纬度","经度","地平线","探测时间","终端IP地址"
						,"加密方式","连接方式","MD5"});
		//QQ账号信息
		String QQ_account_info = csvOutPath+"QQ账号信息.csv";
		CsvWriter QQ_account_info_writer = createCsvWriter(QQ_account_info, 
				new String[] {"序号","QQ号码","昵称","手机号码","年龄","所在地","性别","密码","个性签名","头像","个性签名时间","登录时间"
						,"是否删除","生日","公司","学校","所在地","家乡","星座","个人说明","MD5"});
		
		//QQ好友统计
		String QQ_friends = csvOutPath+"QQ好友统计.csv";
		CsvWriter QQ_friends_writer = createCsvWriter(QQ_friends, 
				new String[] {"序号","好友号码","好友昵称","备注名","个性签名","头像","所在地","性别","是否删除"
						,"年龄","生日","MD5"});
		
		//QQ群组统计
		String QQ_groups = csvOutPath+"QQ群组统计.csv";
		CsvWriter QQ_groups_writer = createCsvWriter(QQ_groups, 
				new String[] {"序号","群号码","群名称","创建人号码","创建人名称","群组号码","群组名称","群描述","群公告","是否删除","MD5"});
		
		//QQ群联系人
		String QQ_group_contacts = csvOutPath+"QQ群联系人.csv";
		CsvWriter QQ_group_contacts_writer = createCsvWriter(QQ_group_contacts, 
				new String[] {"序号","成员号码","成员昵称","备注","群号码","群身份","是否删除","MD5"});
		
		//支付宝账号信息
		String alipay_account_info = csvOutPath+"支付宝账号信息.csv";
		CsvWriter alipay_account_info_writer = createCsvWriter(alipay_account_info, 
				new String[] {"序号","账号","昵称","性别","省"
						,"市","密钥","手势密码","手机","是否删除","头像Url","个性签名","MD5"});
		
		//支付宝好友统计
		String alipay_friends = csvOutPath+"支付宝好友统计.csv";
		CsvWriter alipay_friends_writer = createCsvWriter(alipay_friends, 
				new String[] {"序号","备注名称","昵称","全名","认证状态","帐号","会员等级","地区","签名","手机联系人","手机号"
						,"头像","是否删除","MD5"});
		
		//支付宝群组统计
		String alipay_groups = csvOutPath+"支付宝群组统计.csv";
		CsvWriter alipay_groups_writer = createCsvWriter(alipay_groups, 
				new String[] {"序号","群账号","群名","群主","头像","是否删除","MD5"});
		
		//支付宝群联系人
		String alipay_group_contacts = csvOutPath+"支付宝群联系人.csv";
		CsvWriter alipay_group_contacts_writer = createCsvWriter(alipay_group_contacts, 
				new String[] {"序号","群id","成员id","成员名","是否删除","MD5"});
		
		//微信聊天记录
		String wx_chatrecord = csvOutPath+"微信聊天记录.csv";
		CsvWriter wx_chatrecord_writer = createCsvWriter(wx_chatrecord, 
				new String[] {"好友","发送者","内容","时间"});
		
		//QQ聊天记录
		String QQ_chatrecord = csvOutPath+"QQ聊天记录.csv";
		CsvWriter QQ_chatrecord_writer = createCsvWriter(QQ_chatrecord, 
				new String[] {"好友","发送者","内容","时间"});
		
		//支付宝聊天记录
		String alipay_chatrecord = csvOutPath+"支付宝聊天记录.csv";
		CsvWriter alipay_chatrecord_writer = createCsvWriter(alipay_chatrecord, 
				new String[] {"好友","发送者","内容","时间"});
		
		//微信群聊记录
		String wx_group_chatrecord = csvOutPath+"微信群聊记录.csv";
		CsvWriter wx_group_chatrecord_writer = createCsvWriter(wx_group_chatrecord, 
				new String[] {"微信群","发送者","内容","时间"});
		//QQ群聊记录
		String QQ_group_chatrecord = csvOutPath+"QQ群聊记录.csv";
		CsvWriter QQ_group_chatrecord_writer = createCsvWriter(QQ_group_chatrecord, 
				new String[] {"QQ群","发送者","内容","时间"});
		
		//支付宝群聊记录
		String alipay_group_chatrecord = csvOutPath+"支付宝群聊记录.csv";
		CsvWriter alipay_group_chatrecord_writer = createCsvWriter(alipay_group_chatrecord, 
				new String[] {"支付宝群","发送者","内容","时间"});
		String account_name = "";
		for(String html : htmls) {
			
			File input = new File(path+html);
			
			Document document = Jsoup.parse(input,"UTF-8");
			//所有表格都在  class为OuterTable 的table标签中
			//获取所有OuterTable
			Elements OuterTable = document.getElementsByClass("OuterTable");
			
			
			//遍历所有OuterTable，将数据对号入座
			for(Element e : OuterTable) {
				
				
				//根据tableheader 判断表格类型
				Element tableheader = null;
				if(e.getElementsByClass("tableheader").size()>0) {
					tableheader = e.getElementsByClass("tableheader").get(0);
				}

				if(tableheader != null) {//没有tableheader的表格是短信聊天table
					Elements td = tableheader.getElementsByTag("td");
					
					//应用列表
					if(td.size()==3 && td.get(0).ownText().equals("序号") &&
							td.get(1).ownText().equals("名称") &&
							td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							
							String[] content = {tr.getElementsByTag("td").get(0).ownText(),
									tr.getElementsByTag("td").get(1).ownText()};
							applist_writer.writeRecord(content);
							tr = tr.nextElementSibling();
						}
						
					}
					
					//通讯录
					if(td.size()==6 && td.get(1).ownText().equals("姓名") &&
							td.get(2).ownText().equals("电话")) {
						Element tr = tableheader.nextElementSibling();
						while(tr!=null) {
							String[] content = {tr.getElementsByTag("td").get(0).ownText(),
									tr.getElementsByTag("td").get(1).ownText(),
									tr.getElementsByTag("td").get(2).ownText(),
									tr.getElementsByTag("td").get(3).ownText(),
									tr.getElementsByTag("td").get(4).ownText(),
									tr.getElementsByTag("td").get(5).ownText()};
							contacts_writer.writeRecord(content);
							tr = tr.nextElementSibling();
						}
						
					}
					
					//通话记录
					if(td.size()==7 && td.get(1).ownText().equals("姓名") && td.get(3).ownText().equals("通话时间")) {
						Element tr = tableheader.nextElementSibling();
						while(tr!=null) {
						String[] content = {tr.getElementsByTag("td").get(0).ownText(),
								tr.getElementsByTag("td").get(1).ownText(),
								tr.getElementsByTag("td").get(2).ownText(),
								tr.getElementsByTag("td").get(3).ownText(),
								tr.getElementsByTag("td").get(4).ownText(),
								tr.getElementsByTag("td").get(5).ownText(),
								tr.getElementsByTag("td").get(6).ownText()};
						calls_writer.writeRecord(content);
						tr = tr.nextElementSibling();
						
					   }
				    }
					
					//微信账号信息
					if(td.size()==3 && td.get(1).ownText().equals("微信号") && td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String account = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							String nickname = temp.get(0).getElementsByTag("td").get(1).ownText();
							String id = temp.get(1).getElementsByTag("td").get(1).ownText();
							String qq = temp.get(2).getElementsByTag("td").get(1).ownText();
							String psword = temp.get(3).getElementsByTag("td").get(1).ownText();
							String sex = temp.get(4).getElementsByTag("td").get(1).ownText();
							String signature = temp.get(5).getElementsByTag("td").get(1).ownText();
							String note = temp.get(6).getElementsByTag("td").get(1).ownText();
							String province = temp.get(7).getElementsByTag("td").get(1).ownText();
							String city = temp.get(8).getElementsByTag("td").get(1).ownText();
							String profile = temp.get(9).getElementsByTag("td").get(1).ownText();
							String email = temp.get(10).getElementsByTag("td").get(1).ownText();
							String login_time = temp.get(11).getElementsByTag("td").get(1).ownText();
							String is_delete = temp.get(12).getElementsByTag("td").get(1).ownText();
							String country = temp.get(13).getElementsByTag("td").get(1).ownText();
							String phone = temp.get(14).getElementsByTag("td").get(1).ownText();
							String MD5 = temp.get(15).getElementsByTag("td").get(1).ownText();
							String[] content = {sequence_num,account,nickname,id,qq,psword,
									sex,signature,note,province,city,profile,email,login_time,
									is_delete,country,phone,MD5};
							wx_account_info_writer.writeRecord(content);
							
							tr = tr.nextElementSibling();
						}
					}
					//wx好友统计
					if(td.size()==3 && td.get(1).ownText().equals("微信ID") && td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							
							
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String id = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							if(temp.size()==16) {
								String account = temp.get(0).getElementsByTag("td").get(1).ownText();
								String nickname = temp.get(1).getElementsByTag("td").get(1).ownText();
								String sex = temp.get(2).getElementsByTag("td").get(1).ownText();
								String sigature = temp.get(3).getElementsByTag("td").get(1).ownText();
								String note = temp.get(4).getElementsByTag("td").get(1).ownText();
								String address = temp.get(5).getElementsByTag("td").get(1).ownText();
								String profile = temp.get(6).getElementsByTag("td").get(1).ownText();
								String QQ = temp.get(7).getElementsByTag("td").get(1).ownText();
								String QQaccount = temp.get(8).getElementsByTag("td").get(1).ownText();
								String email = temp.get(9).getElementsByTag("td").get(1).ownText();
								String phone = temp.get(10).getElementsByTag("td").get(1).ownText();
								String is_delete = temp.get(11).getElementsByTag("td").get(1).ownText();
								String des = temp.get(12).getElementsByTag("td").get(1).ownText();
								String tag = temp.get(13).getElementsByTag("td").get(1).ownText();
								String common_chat = temp.get(14).getElementsByTag("td").get(1).ownText();
								String MD5 = temp.get(15).getElementsByTag("td").get(1).ownText();
								String[] content = {sequence_num,id,
										account,nickname,sex,sigature,note,address,profile,QQ,QQaccount,
										email,phone,is_delete,des,tag,common_chat,MD5};
								wx_friends_writer.writeRecord(content);
							}
							tr = tr.nextElementSibling();
							
						}
						
					}
					//微信群组统计
					if(td.size()==3 && td.get(1).ownText().equals("群帐号") && td.get(2).ownText().equals("条目")) {
						
						Element tr = tableheader.nextElementSibling();
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String account = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							if(temp.size()==7) {
								String name = temp.get(0).getElementsByTag("td").get(1).ownText();
								String owner = temp.get(1).getElementsByTag("td").get(1).ownText();
								String ownerNickname = temp.get(2).getElementsByTag("td").get(1).ownText();
								String count = temp.get(3).getElementsByTag("td").get(1).ownText();
								String nickname = temp.get(4).getElementsByTag("td").get(1).ownText();
								String is_delete = temp.get(5).getElementsByTag("td").get(1).ownText();
								String MD5= temp.get(6).getElementsByTag("td").get(1).ownText();
								String[] content = {sequence_num,account,name,owner,ownerNickname,count,
										nickname,is_delete,MD5};
								wx_groups_writer.writeRecord(content);
							}else if (temp.size()==10) {
								String name = temp.get(0).getElementsByTag("td").get(1).ownText();
								String member = temp.get(1).getElementsByTag("td").get(1).ownText();
								String member_account = temp.get(2).getElementsByTag("td").get(1).ownText();
								String is_delete = temp.get(3).getElementsByTag("td").get(1).ownText();
								String group_nickname = temp.get(4).getElementsByTag("td").get(1).ownText();
								String signature = temp.get(5).getElementsByTag("td").get(1).ownText();
								String location= temp.get(6).getElementsByTag("td").get(1).ownText();
								String profile= temp.get(7).getElementsByTag("td").get(1).ownText();
								String reason_in= temp.get(8).getElementsByTag("td").get(1).ownText();
								String MD5= temp.get(9).getElementsByTag("td").get(1).ownText();
								String[] content = {sequence_num,account,name,member,member_account,
										is_delete,group_nickname,signature,location,profile,reason_in,MD5};
								wx_group_contacts_writer.writeRecord(content);
								
							}
							
							
							tr=tr.nextElementSibling();
						}
					}
					//无线连接
					if(td.size()==3 && td.get(1).ownText().equals("MAC") && td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String MAC = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							String name = temp.get(0).getElementsByTag("td").get(1).ownText();
							String last_login = temp.get(1).getElementsByTag("td").get(1).ownText();
							String w = temp.get(2).getElementsByTag("td").get(1).ownText();
							String j = temp.get(3).getElementsByTag("td").get(1).ownText();
							String d = temp.get(4).getElementsByTag("td").get(1).ownText();
							String used_time = temp.get(5).getElementsByTag("td").get(1).ownText();
							String ip = temp.get(6).getElementsByTag("td").get(1).ownText();
							String enc = temp.get(7).getElementsByTag("td").get(1).ownText();
							String login_type = temp.get(8).getElementsByTag("td").get(1).ownText();
							String MD5 = temp.get(9).getElementsByTag("td").get(1).ownText();
							String[] content = {sequence_num,MAC,name,last_login,w,j,d,used_time,ip,enc,login_type,MD5};
							wifi_account_info_writer.writeRecord(content);
							tr=tr.nextElementSibling();
						}
					

					}	
					//QQ账号信息
					if(td.size()==3 && td.get(1).ownText().equals("QQ号码") && td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String QQ = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							String nickname = temp.get(0).getElementsByTag("td").get(1).ownText();
							String phone = temp.get(1).getElementsByTag("td").get(1).ownText();
							String age = temp.get(2).getElementsByTag("td").get(1).ownText();
							String location = temp.get(3).getElementsByTag("td").get(1).ownText();
							String sex = temp.get(4).getElementsByTag("td").get(1).ownText();
							String psword = temp.get(5).getElementsByTag("td").get(1).ownText();
							String signature = temp.get(6).getElementsByTag("td").get(1).ownText();
							String profile = temp.get(7).getElementsByTag("td").get(1).ownText();
							String signature_time = temp.get(8).getElementsByTag("td").get(1).ownText();
							String login_time = temp.get(9).getElementsByTag("td").get(1).ownText();
							String is_delete = temp.get(10).getElementsByTag("td").get(1).ownText();
							String born = temp.get(11).getElementsByTag("td").get(1).ownText();
							String company = temp.get(12).getElementsByTag("td").get(1).ownText();
							String school = temp.get(13).getElementsByTag("td").get(1).ownText();
							String location2 = temp.get(14).getElementsByTag("td").get(1).ownText();
							String home = temp.get(15).getElementsByTag("td").get(1).ownText();
							String constellation = temp.get(16).getElementsByTag("td").get(1).ownText();
							String statement = temp.get(17).getElementsByTag("td").get(1).ownText();
							String MD5 = temp.get(18).getElementsByTag("td").get(1).ownText();
							
							String[] content = {sequence_num,QQ,nickname,phone,age,
									location,sex,psword,signature,profile,signature_time,login_time,is_delete
									,born,company,school,location2,home,constellation,statement,MD5};
							QQ_account_info_writer.writeRecord(content);
							
							
							tr = tr.nextElementSibling();
							
						}
					}
					//QQ好友统计
					if(td.size()==3 && td.get(1).ownText().equals("好友号码") && td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							
							
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String QQ = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							if(temp.size()==11) {
								String t1 = temp.get(0).getElementsByTag("td").get(1).ownText();
								String t2 = temp.get(1).getElementsByTag("td").get(1).ownText();
								String t3 = temp.get(2).getElementsByTag("td").get(1).ownText();
								String t4 = temp.get(3).getElementsByTag("td").get(1).ownText();
								String t5 = temp.get(4).getElementsByTag("td").get(1).ownText();
								String t6 = temp.get(5).getElementsByTag("td").get(1).ownText();
								String t7 = temp.get(6).getElementsByTag("td").get(1).ownText();
								String t8 = temp.get(7).getElementsByTag("td").get(1).ownText();
								String t9 = temp.get(8).getElementsByTag("td").get(1).ownText();
								String t10 = temp.get(9).getElementsByTag("td").get(1).ownText();
								String t11 = temp.get(10).getElementsByTag("td").get(1).ownText();
								
								String[] content = {sequence_num,QQ,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11};
								QQ_friends_writer.writeRecord(content);
							}
							tr = tr.nextElementSibling();
							
						}
						
					}
					//QQ群组统计
					if(td.size()==3 && td.get(1).ownText().equals("群号码") && td.get(2).ownText().equals("条目")) {
						
						Element tr = tableheader.nextElementSibling();
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String QQ = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							if(temp.size()==9) {
								String t1 = temp.get(0).getElementsByTag("td").get(1).ownText();
								String t2 = temp.get(1).getElementsByTag("td").get(1).ownText();
								String t3 = temp.get(2).getElementsByTag("td").get(1).ownText();
								String t4 = temp.get(3).getElementsByTag("td").get(1).ownText();
								String t5 = temp.get(4).getElementsByTag("td").get(1).ownText();
								String t6 = temp.get(5).getElementsByTag("td").get(1).ownText();
								String t7 = temp.get(6).getElementsByTag("td").get(1).ownText();
								String t8 = temp.get(7).getElementsByTag("td").get(1).ownText();
								String t9 = temp.get(8).getElementsByTag("td").get(1).ownText();
								
								String[] content = {sequence_num,QQ,t1,t2,t3,t4,t5,t6,t7,t8,t9};
								QQ_groups_writer.writeRecord(content);
							}
							tr=tr.nextElementSibling();
						}
					}
					
					//QQ群联系人统计
					if(td.size()==3 && td.get(1).ownText().equals("成员号码") && td.get(2).ownText().equals("条目")) {
						
						Element tr = tableheader.nextElementSibling();
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String QQ = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							if(temp.size()==6) {
								String t1 = temp.get(0).getElementsByTag("td").get(1).ownText();
								String t2 = temp.get(1).getElementsByTag("td").get(1).ownText();
								String t3 = temp.get(2).getElementsByTag("td").get(1).ownText();
								String t4 = temp.get(3).getElementsByTag("td").get(1).ownText();
								String t5 = temp.get(4).getElementsByTag("td").get(1).ownText();
								String t6 = temp.get(5).getElementsByTag("td").get(1).ownText();
								
								
								String[] content = {sequence_num,QQ,t1,t2,t3,t4,t5,t6};
								QQ_group_contacts_writer.writeRecord(content);
							}
							tr=tr.nextElementSibling();
						}
					}
					
					//支付宝账号信息
					if(td.size()==3 && td.get(1).ownText().equals("帐号") && td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String account = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							
							if(temp.size()==11) {
								String t1 = temp.get(0).getElementsByTag("td").get(1).ownText();
								String t2 = temp.get(1).getElementsByTag("td").get(1).ownText();
								String t3 = temp.get(2).getElementsByTag("td").get(1).ownText();
								String t4 = temp.get(3).getElementsByTag("td").get(1).ownText();
								String t5 = temp.get(4).getElementsByTag("td").get(1).ownText();
								String t6 = temp.get(5).getElementsByTag("td").get(1).ownText();
								String t7 = temp.get(6).getElementsByTag("td").get(1).ownText();
								String t8 = temp.get(7).getElementsByTag("td").get(1).ownText();
								String t9 = temp.get(8).getElementsByTag("td").get(1).ownText();
								String t10 = temp.get(9).getElementsByTag("td").get(1).ownText();
								String t11 = temp.get(10).getElementsByTag("td").get(1).ownText();
								String[] content = {sequence_num,account,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11};
								alipay_account_info_writer.writeRecord(content);
							}
							tr=tr.nextElementSibling();
						}
						
					}
					//支付宝好友统计
					if(td.size()==3 && td.get(1).ownText().equals("备注名称") && td.get(2).ownText().equals("条目")) {
						Element tr = tableheader.nextElementSibling();
						
						while(tr!=null) {
							String sequence_num = tr.getElementsByTag("td").get(0).ownText();
							String account = tr.getElementsByTag("td").get(1).ownText();
							Elements temp = tr.getElementsByTag("tbody").get(0).getElementsByTag("tr");
							
							if(temp.size()==12) {
								String t1 = temp.get(0).getElementsByTag("td").get(1).ownText();
								String t2 = temp.get(1).getElementsByTag("td").get(1).ownText();
								String t3 = temp.get(2).getElementsByTag("td").get(1).ownText();
								String t4 = temp.get(3).getElementsByTag("td").get(1).ownText();
								String t5 = temp.get(4).getElementsByTag("td").get(1).ownText();
								String t6 = temp.get(5).getElementsByTag("td").get(1).ownText();
								String t7 = temp.get(6).getElementsByTag("td").get(1).ownText();
								String t8 = temp.get(7).getElementsByTag("td").get(1).ownText();
								String t9 = temp.get(8).getElementsByTag("td").get(1).ownText();
								String t10 = temp.get(9).getElementsByTag("td").get(1).ownText();
								String t11 = temp.get(10).getElementsByTag("td").get(1).ownText();
								String t12 = temp.get(11).getElementsByTag("td").get(1).ownText();
								String[] content = {sequence_num,account,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12};
								alipay_friends_writer.writeRecord(content);
							}
							tr=tr.nextElementSibling();
						}
						
					}
					//支付宝群组
					if(td.size()==7 && td.get(1).ownText().equals("群帐号") && td.get(2).ownText().equals("群名")) {
						Element tr = tableheader.nextElementSibling();
						while(tr!=null) {
							String[] content = {tr.getElementsByTag("td").get(0).ownText(),
									tr.getElementsByTag("td").get(1).ownText(),
									tr.getElementsByTag("td").get(2).ownText(),
									tr.getElementsByTag("td").get(3).ownText(),
									tr.getElementsByTag("td").get(4).ownText(),
									tr.getElementsByTag("td").get(5).ownText(),
									tr.getElementsByTag("td").get(6).ownText()};
							alipay_groups_writer.writeRecord(content);
							tr=tr.nextElementSibling();
						}
						
					}
					
					//支付宝群联系人
					if(td.size()==6 && td.get(1).ownText().equals("群Id") && td.get(2).ownText().equals("成员Id")) {
						Element tr = tableheader.nextElementSibling();
						while(tr!=null) {
							String[] content = {tr.getElementsByTag("td").get(0).ownText(),
									tr.getElementsByTag("td").get(1).ownText(),
									tr.getElementsByTag("td").get(2).ownText(),
									tr.getElementsByTag("td").get(3).ownText(),
									tr.getElementsByTag("td").get(4).ownText(),
									tr.getElementsByTag("td").get(5).ownText(),
									};
							alipay_group_contacts_writer.writeRecord(content);
							tr=tr.nextElementSibling();
						}
						
					}
				}
				
				//短信
				if(tableheader==null) {
					if(e.attr("bgcolor").equals("#F5FFFA")) {
						
						if(e.getElementById("cmxbox")!=null) {
							
							
							Element tr = e.getElementsByTag("tbody").get(0).getElementsByTag("tr").get(0);
							
							while(tr!=null) {
								//System.out.println(tr);
								
								if(tr.getElementsByClass("DialogTxt2").size()>0) {//别人给机主发短信
									
									String sender = tr.getElementsByClass("DialogTxt2").get(0).ownText();
									String MSContent = tr.getElementsByClass("zqTXT17").get(0).ownText();
									String time = tr.getElementsByClass("zqTXT13").get(0).ownText();
									String[] content = {sender,MSContent,time};
									message_writer.writeRecord(content);
								}else if (tr.getElementsByClass("DialogTxt4").size()>0){//机主给别人发短信
									
									String sender = tr.getElementsByClass("DialogTxt4").get(0).ownText();
									String MSContent = tr.getElementsByClass("zqTXT16").get(0).ownText();
									String time = tr.getElementsByClass("zqTXT14").get(0).ownText();
									String[] content = {sender,MSContent,time};
									message_writer.writeRecord(content);
								}else {
									
									
									
									if(tr.getElementsByClass("zqTXT17").size()>0) {//别人发的消息
										
										if(e.previousElementSibling()!=null) {
											account_name = e.previousElementSibling().getElementsByTag("a").get(0).ownText();
										}
										
										
										String sender = tr.getElementById("cmxbox").parent().parent().previousElementSibling().getElementsByTag("td").get(1).ownText();
										//String reciver = name;
										String MScontent = tr.getElementsByClass("zqTXT17").get(0).ownText();
										String time = tr.getElementsByClass("zqTXT13").get(0).ownText();
										String[] content= {account_name,sender,MScontent,time};
										System.out.println(account_name);
										String[] tmp = account_name.split("/");
										if(isInteger(tmp[0])) {
											if(tmp.length >1) {//帐号有 /线的
												if(tmp[1].contains("、")) {
													System.out.println("我是QQ群聊");
													QQ_group_chatrecord_writer.writeRecord(content);
												}else {
													System.out.println("我是QQ 个人聊天");
													QQ_chatrecord_writer.writeRecord(content);
												}
											}else {
												System.out.println("我是QQ 个人聊天");
												wx_chatrecord_writer.writeRecord(content);
											}
											
										}else {
											if(tmp[0].contains("@chatroom")) {
												System.out.println("我是微信群聊");
												wx_group_chatrecord_writer.writeRecord(content);
											}else if (tmp[0].contains("、")) {
												System.out.println("我是支付宝群聊");
												alipay_group_chatrecord_writer.writeRecord(content);
											}else if (tmp.length>1) {
												if(tmp[1].substring(0, 4).contains("2088")) {
													System.out.println("是支付宝聊天记录");
													alipay_chatrecord_writer.writeRecord(content);
												}else {
													System.out.println("我是微信聊天记录");
													wx_chatrecord_writer.writeRecord(content);
												}
											}
										}
										
									}else if (tr.getElementsByClass("zqTXT16").size()>0) {//发给别人的
										if(e.previousElementSibling()!=null) {
											account_name = e.previousElementSibling().getElementsByTag("a").get(0).ownText();
										}
										
										String sender = tr.getElementById("cmxbox").parent().parent().previousElementSibling().getElementsByTag("td").get(0).ownText();
										
										String MSContent = tr.getElementsByClass("zqTXT16").get(0).ownText();
										String time = tr.getElementsByClass("zqTXT14").get(0).ownText();
										String[] content= {account_name,sender,MSContent,time};
										System.out.println(account_name);
										String[] tmp = account_name.split("/");
										if(isInteger(tmp[0])) {
											if(tmp.length >1) {
												if(tmp[1].contains("、")) {
													System.out.println("我是QQ群聊");
													QQ_group_chatrecord_writer.writeRecord(content);
												}else {
													System.out.println("我是QQ 个人聊天");
													QQ_chatrecord_writer.writeRecord(content);
												}
											}else {
												System.out.println("我是QQ 个人聊天");
												wx_chatrecord_writer.writeRecord(content);
											}
											
										}else {
											if(tmp[0].contains("@chatroom")) {
												System.out.println("我是微信群聊");
												wx_group_chatrecord_writer.writeRecord(content);
											}else if (tmp[0].contains("、")) {
												System.out.println("我是支付宝群聊");
												alipay_group_chatrecord_writer.writeRecord(content);
											}else if (tmp.length>1) {
												if(tmp[1].substring(0, 4).contains("2088")) {
													System.out.println("是支付宝聊天记录");
													alipay_chatrecord_writer.writeRecord(content);
												}else {
													System.out.println("我是微信聊天记录");
													wx_chatrecord_writer.writeRecord(content);
												}
											}
										}	
									}
									
								}
								tr= tr.nextElementSibling();
								
							}
						}
					}
				}
			}	
		}
		List<CsvWriter> list = new ArrayList<>();
		list.add(applist_writer);
		list.add(contacts_writer);
		list.add(calls_writer);
		list.add(message_writer);
		list.add(wx_account_info_writer);
		list.add(wx_friends_writer);
		list.add(wx_groups_writer);
		list.add(wx_group_contacts_writer);
		list.add(wifi_account_info_writer);
		list.add(QQ_account_info_writer);
		list.add(QQ_friends_writer);
		list.add(QQ_groups_writer);
		list.add(QQ_group_contacts_writer);
		list.add(alipay_account_info_writer);
		list.add(alipay_friends_writer);
		list.add(alipay_groups_writer);
		list.add(alipay_group_contacts_writer);
		list.add(wx_chatrecord_writer);
		list.add(wx_group_chatrecord_writer);
		list.add(QQ_chatrecord_writer);
		list.add(QQ_group_chatrecord_writer);
		list.add(alipay_chatrecord_writer);
		list.add(alipay_group_chatrecord_writer);
		closeCsvWriter(list);
		
		//删除不需要的文件
		if(!get_applist) {
			deleteFile(applist);
		}
		if(!get_contacts) {
			deleteFile(contacts);
		}
		if(!get_callsRecord) {
			deleteFile(calls);
		}
		if(!get_wechat) {
			deleteFile(wx_account_info);
			deleteFile(wx_friends);
			deleteFile(wx_groups);
			deleteFile(wx_group_contacts);
			deleteFile(wx_chatrecord);
			deleteFile(wx_group_chatrecord);
		}
		if(!get_QQ) {
			
			deleteFile(QQ_account_info);
			deleteFile(QQ_friends);
			deleteFile(QQ_groups);
			deleteFile(QQ_group_contacts);
			deleteFile(QQ_chatrecord);
			deleteFile(QQ_group_chatrecord);
		}
		if(!get_alipay) {
			
			deleteFile(alipay_account_info);
			deleteFile(alipay_friends);
			deleteFile(alipay_groups);
			deleteFile(alipay_group_contacts);
			deleteFile(alipay_chatrecord);
			deleteFile(alipay_group_chatrecord);
		}
		if(!get_systemInfo) {
			deleteFile(wifi_account_info);
		}

	}

	public static void main(String[] args) {
		//输入取证报告的Report目录即可
		String path = "D:/AHTMLDATA/苹果/Report/";
		System.out.println("start ");
		
		try {
			//outCsv(path, "D:/csvPath2/");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*List<String>  list = getHtml(path);
		for(String string : list) {
			System.out.println(string);
		}*/
	}
	
}
